import {
  hasChild,
  localRead
} from '@/libs/util'
import Main from '@/components/main'
import parentView from '@/components/parent-view'
import {
  forEach
} from '@/libs/tools'

export const LocalRouter = 'CacheRouter'
export const defaultNames = ['login', 'home', 'bigdata', 'index', 'articleshowlist', 'updatepwd','uploadvoice', 'refresh', 'error_401', 'error_404', 'error_500']

/**
 * 数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 * @param level 层级
 */
const listToTree = (list, tree, parentId, level) => {
  if (list) {
    list.forEach(item => {
      item.level = level
      // 判断是否为父级菜单
      // eslint-disable-next-line eqeqeq
      if (item.parentid == parentId) {
        const child = {
          ...item,
          key: item.key || item.name,
          children: []
        }
        // 迭代 list， 找到当前菜单相符合的所有子菜单
        listToTree(list, child.children, item.id, (level + 1))
        // 删掉不存在 children 值的属性
        if (child.children && child.children.length <= 0) {
          delete child.children
        }
        // 加入到树中
        tree.push(child)
      }
    })
  }

}
/**
 * 动态生成菜单
 * @param data
 * @returns {Promise<Router>}
 */
// export const generatorDynamicRouter = (data) => {
//   const resNav = data
//   const childrenNav = [] 
//   // 后端数据, 根级树数组,   PID，根级
//   listToTree(resNav, childrenNav, 0, 0)

  
//   var finalList = [] 
 
//   var key = sessionStorage.getItem('globalKey')
//   if (key) {
//     var parent = childrenNav.find(x => x.name == key)
//     if (parent && parent.children) {
//       finalList.push(...parent.children) 
//     } 
//   }
//   var pubList = childrenNav.find(x => x.name == 'pub')
//   if (pubList && pubList.children) {
//     finalList.push(...pubList.children)
//   } 

//   return formatMenu(finalList)
// }

/**
 * 动态生成菜单
 * @param data
 * @returns {Promise<Router>}
 */
export const generatorDynamicRouter = (data) => {
 
  const resNav = data
  const childrenNav = []
  // 后端数据, 根级树数组,  根级 PID
  listToTree(resNav, childrenNav, 0)

  return formatMenu(childrenNav,0)
}

// 加载路由
export const loadRouter = () => {
  let data = localRead(LocalRouter)
  if (data && data != 'undefined') {
    let list = JSON.parse(data)
    return generatorDynamicRouter(list)
  } else {
    return []
  }
}

// 格式化菜单
export const formatMenu = (list, level = 0) => {
  if (!list) {
    return []
  }
  let res = []
  
  forEach(list, item => { 
    let obj = {
      path: item.path,
      name: item.name,
      props: item.props,
      propstitle: item.propstitle
    }
    obj.meta = item.meta
    if (obj.props && obj.propstitle) {
      obj.meta.title = route => route.params.title
    }
    if (item.propsinfo && item.propsinfo.indexOf('{') > -1) {
      obj.props = JSON.parse(item.propsinfo)
    }
    item.level = level
    if (hasChild(item)) {
      if (item.level >0) {
        obj.component = parentView
      } else {
        obj.component = Main
      }

    } else {
      obj.component = () => import('@/view/' + item.component)
    }
    if (hasChild(item)) {
      obj.children = formatMenu(item.children, level + 1); 
    }
    res.push(obj)
  })   
  return res
  
}
